﻿using ComLib.ComType;
using KXmlUIControl;
using KWindow.KPageCore;
using System;
using System.Diagnostics;
using System.Windows.Forms;

namespace IPCPages
{
    public partial class PageSelectApply : PageControlBase
    {     
        /// <summary>
        /// 连续点击激活次数统计
        /// </summary>
        private int active_click_count = 0;
        private Stopwatch sw = new Stopwatch();
        //private PictureBox pictureBox1 = null;
        //private PictureBox pictureBoxBackground = null;
       //private Button buttonStart = null;
        private Button buttonCrj = null;
        private Button buttonHz = null;
        private Button buttonJg = null;

        public PageSelectApply()
        {       
            InitializeComponent();
            SetProcessBase(ProcessSelectApply.Instance);
        }

        private void GetControls()
        {
            labelTitle = GetControl<Label>("labelTitle") as Label;
            //pictureBox1 = GetControl<PictureBox>("pictureBox1") as PictureBox;
            //pictureBoxBackground = GetControl<PictureBox>("pictureBoxBackground") as PictureBox;
            //buttonStart = GetControl<Button>("buttonStart") as Button;
            //SetButtonDefaultStyles(buttonStart);
            buttonCrj = GetControl<Button>("buttonCrj") as Button;
            SetButtonDefaultStyles(buttonCrj);
            buttonHz = GetControl<Button>("buttonHz") as Button;
            SetButtonDefaultStyles(buttonHz);
            buttonJg = GetControl<Button>("buttonJg") as Button;
            SetButtonDefaultStyles(buttonJg);
        }

        //初始化窗体数据
        public override void InitCtrls()
        {
            base.InitCtrls();
            GetControls();
            //this.Controls.Clear();
            //pictureBox1.Parent = pictureBoxBackground;
            //buttonStart.Parent = pictureBoxBackground;
        }

        //初始化窗体数据
        public override void InitData()
        {
            SD.ClearValue();
            Process.SetValue("ActionIndex", 1);
        }

        /// <summary>
        /// 保存窗体数据(窗体本次做完后, 进入下一步之前)
        /// </summary>
        public override void UpdateData()
        {
            
        }

        /// <summary>
        /// 清理窗体数据(窗体本次做完后)
        /// </summary>
        public override void ClearData()
        {
            base.ClearData();
        }

        /// <summary>
        /// 窗体关闭时
        /// </summary>
        public override void Close()
        {
            base.Close();
        }

        public override void StartWork()
        {
            //WebDataAccess webAccess = WebDataAccess.Instance;
            //if (webAccess.Enabled && !webAccess.Inited)
            //{
            //    Task.Factory.StartNew(() =>
            //    {
            //        if (!webAccess.Login())
            //        {
            //            string strLog = "系统登录失败:" + webAccess.ErrorMessage;
            //            Log.WriteAsync(strLog);
            //            ShowLoginFailedMessage(webAccess.ErrorMessage);
            //        }
            //    });
            //}            
        }

        private void ShowLoginFailedMessage(string error)
        {
            Action<string> handler = (e) =>
            {
                MessageBox.Show(e, "全国版系统登录失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
            };
            this.Invoke(handler, error);
        }

        public override void StopWork()
        {
            base.StopWork();
        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            Process.ToNext();
        }

        private void buttonCrj_Click(object sender, EventArgs e)
        {
            Process.SetValue("ApplyTo", ApplyTo.ExitAndEntry);
            Process.ToNext();
        }

        private void buttonHz_Click(object sender, EventArgs e)
        {
            Process.SetValue("ApplyTo", ApplyTo.IDCard);
            Process.ToNext();
        }

        private void buttonJg_Click(object sender, EventArgs e)
        {
            Process.SetValue("ApplyTo", ApplyTo.DrivingLicence);
            Process.ToNext();
        }

        //private void pictureBox1_Click(object sender, EventArgs e)
        //{
        //    if (!sw.IsRunning)
        //    {
        //        sw.Restart();
        //        return;
        //    }
        //    if (sw.ElapsedMilliseconds > 2000)
        //    {
        //        sw.Reset();
        //        active_click_count = 0;
        //    }
        //    else
        //    {
        //        active_click_count++;
        //    }
        //    if (active_click_count == 2)
        //    {
        //        sw.Reset();
        //        active_click_count = 0;
        //        Log.WriteAsync("用户打开了管理员选项");
        //        FormAdministrator form = new FormAdministrator();
        //        if (form.ShowDialog() == DialogResult.OK)
        //        {
        //            Log.WriteAsync("即将执行管理员选项动作:" + form.SelectAction.ToString());
        //            Process.OnTakeAdminAction(form.SelectAction);
        //        }
        //    }
        //}
    }
}
